﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class ComponentView : View
    {
        protected List<View> _subViews;

        public ComponentView()
        {
            _subViews = new List<View>();
        }

        public ComponentView(string strTexture, Vector2 pos, Vector2 size)
        {
            _subViews = new List<View>();
            _background = GameBase.Constant.content.Load<Texture2D>(strTexture);
            Position = pos;
            Size = size;
        }

        public override Vector2 Position
        {
            get { return base.Position; }
            set
            {
                Vector2 deltaPos = value - base.Position;
                base.Position = value;
                foreach (View view in _subViews)
                    view.Position += deltaPos;
            }
        }

        public void addToSubView(View subView)
        {
            _subViews.Add(subView);
            subView.Position += Position;
        }

        public void addToSubViewAtIndex(View subView, int index)
        {
            _subViews.Insert(index, subView);
            subView.Position += Position;
        }

        public void removeSubViewAtIndex(int index)
        {
            if (index >= 0 && index < _subViews.Count)
            {
                _subViews[index].Position -= Position;
                _subViews.RemoveAt(index);
            }
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color color)
        {
            base.draw(gameTime, spriteBatch, ColorForDraw);

            foreach (View view in _subViews)
                if(view.Visible)
                    view.draw(gameTime, spriteBatch, color);
        }

        public override void update(GameTime gameTime)
        {
            foreach (View view in _subViews)
                view.update(gameTime);
            base.update(gameTime);
        }

        public override float Rotation
        {
            get { return _rotation; }
            set
            {
                _rotation = value;
                foreach (View view in _subViews)
                    view.Rotation = value;
            }
        }
    }
}
